home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / dis.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  6.3 KB  |  245 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import sys
  5. import types
  6. from opcode import *
  7. from opcode import __all__ as _opcodes_all
  8. __all__ = [
  9.     'dis',
  10.     'disassemble',
  11.     'distb',
  12.     'disco'] + _opcodes_all
  13. del _opcodes_all
  14.  
  15. def dis(x = None):
  16.     if x is None:
  17.         distb()
  18.         return None
  19.     
  20.     if type(x) is types.InstanceType:
  21.         x = x.__class__
  22.     
  23.     if hasattr(x, 'im_func'):
  24.         x = x.im_func
  25.     
  26.     if hasattr(x, 'func_code'):
  27.         x = x.func_code
  28.     
  29.     if hasattr(x, '__dict__'):
  30.         items = x.__dict__.items()
  31.         items.sort()
  32.         for name, x1 in items:
  33.             if type(x1) in (types.MethodType, types.FunctionType, types.CodeType, types.ClassType):
  34.                 print 'Disassembly of %s:' % name
  35.                 
  36.                 try:
  37.                     dis(x1)
  38.                 except TypeError:
  39.                     msg = None
  40.                     print 'Sorry:', msg
  41.  
  42.                 print 
  43.                 continue
  44.         
  45.     elif hasattr(x, 'co_code'):
  46.         disassemble(x)
  47.     elif isinstance(x, str):
  48.         disassemble_string(x)
  49.     else:
  50.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  51.  
  52.  
  53. def distb(tb = None):
  54.     if tb is None:
  55.         
  56.         try:
  57.             tb = sys.last_traceback
  58.         except AttributeError:
  59.             raise RuntimeError, 'no last traceback to disassemble'
  60.  
  61.         while tb.tb_next:
  62.             tb = tb.tb_next
  63.     
  64.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  65.  
  66.  
  67. def disassemble(co, lasti = -1):
  68.     code = co.co_code
  69.     byte_increments = [ ord(c) for c in co.co_lnotab[0::2] ]
  70.     line_increments = [ ord(c) for c in co.co_lnotab[1::2] ]
  71.     table_length = len(byte_increments)
  72.     lineno = co.co_firstlineno
  73.     table_index = 0
  74.     while table_index < table_length and byte_increments[table_index] == 0:
  75.         lineno += line_increments[table_index]
  76.         table_index += 1
  77.         continue
  78.         []
  79.     addr = 0
  80.     line_incr = 0
  81.     labels = findlabels(code)
  82.     n = len(code)
  83.     i = 0
  84.     extended_arg = 0
  85.     free = None
  86.     while i < n:
  87.         c = code[i]
  88.         op = ord(c)
  89.         if i >= addr:
  90.             lineno += line_incr
  91.             while table_index < table_length:
  92.                 addr += byte_increments[table_index]
  93.                 line_incr = line_increments[table_index]
  94.                 table_index += 1
  95.                 if line_incr:
  96.                     break
  97.                     continue
  98.                 []
  99.                 continue
  100.                 []
  101.             addr = sys.maxint
  102.             print '%3d' % lineno,
  103.         else:
  104.             print '   ',
  105.         if i == lasti:
  106.             print '-->',
  107.         else:
  108.             print '   ',
  109.         if i in labels:
  110.             print '>>',
  111.         else:
  112.             print '  ',
  113.         print `i`.rjust(4), opname[op].ljust(20),
  114.         i = i + 1
  115.         if op >= HAVE_ARGUMENT:
  116.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  117.             extended_arg = 0
  118.             i = i + 2
  119.             if op == EXTENDED_ARG:
  120.                 extended_arg = oparg * 0x10000L
  121.             
  122.             print `oparg`.rjust(5),
  123.             if op in hasconst:
  124.                 print '(' + `co.co_consts[oparg]` + ')',
  125.             elif op in hasname:
  126.                 print '(' + co.co_names[oparg] + ')',
  127.             elif op in hasjrel:
  128.                 print '(to ' + `i + oparg` + ')',
  129.             elif op in haslocal:
  130.                 print '(' + co.co_varnames[oparg] + ')',
  131.             elif op in hascompare:
  132.                 print '(' + cmp_op[oparg] + ')',
  133.             elif op in hasfree:
  134.                 if free is None:
  135.                     free = co.co_cellvars + co.co_freevars
  136.                 
  137.                 print '(' + free[oparg] + ')',
  138.             
  139.         
  140.         print 
  141.  
  142.  
  143. def disassemble_string(code, lasti = -1, varnames = None, names = None, constants = None):
  144.     labels = findlabels(code)
  145.     n = len(code)
  146.     i = 0
  147.     while i < n:
  148.         c = code[i]
  149.         op = ord(c)
  150.         if op == opmap['SET_LINENO'] and i > 0:
  151.             print 
  152.         
  153.         if i == lasti:
  154.             print '-->',
  155.         else:
  156.             print '   ',
  157.         if i in labels:
  158.             print '>>',
  159.         else:
  160.             print '  ',
  161.         print `i`.rjust(4), opname[op].ljust(15),
  162.         i = i + 1
  163.         if op >= HAVE_ARGUMENT:
  164.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  165.             i = i + 2
  166.             print `oparg`.rjust(5),
  167.             if op in hasconst:
  168.                 if constants:
  169.                     print '(' + `constants[oparg]` + ')',
  170.                 else:
  171.                     print '(%d)' % oparg,
  172.             elif op in hasname:
  173.                 if names is not None:
  174.                     print '(' + names[oparg] + ')',
  175.                 else:
  176.                     print '(%d)' % oparg,
  177.             elif op in hasjrel:
  178.                 print '(to ' + `i + oparg` + ')',
  179.             elif op in haslocal:
  180.                 if varnames:
  181.                     print '(' + varnames[oparg] + ')',
  182.                 else:
  183.                     print '(%d)' % oparg,
  184.             elif op in hascompare:
  185.                 print '(' + cmp_op[oparg] + ')',
  186.             
  187.         
  188.         print 
  189.  
  190. disco = disassemble
  191.  
  192. def findlabels(code):
  193.     labels = []
  194.     n = len(code)
  195.     i = 0
  196.     while i < n:
  197.         c = code[i]
  198.         op = ord(c)
  199.         i = i + 1
  200.         if op >= HAVE_ARGUMENT:
  201.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  202.             i = i + 2
  203.             label = -1
  204.             if op in hasjrel:
  205.                 label = i + oparg
  206.             elif op in hasjabs:
  207.                 label = oparg
  208.             
  209.             if label >= 0:
  210.                 if label not in labels:
  211.                     labels.append(label)
  212.                 
  213.             
  214.         label >= 0
  215.     return labels
  216.  
  217.  
  218. def _test():
  219.     if sys.argv[1:]:
  220.         if sys.argv[2:]:
  221.             sys.stderr.write('usage: python dis.py [-|file]\n')
  222.             sys.exit(2)
  223.         
  224.         fn = sys.argv[1]
  225.         if not fn or fn == '-':
  226.             fn = None
  227.         
  228.     else:
  229.         fn = None
  230.     if fn is None:
  231.         f = sys.stdin
  232.     else:
  233.         f = open(fn)
  234.     source = f.read()
  235.     if fn is not None:
  236.         f.close()
  237.     else:
  238.         fn = '<stdin>'
  239.     code = compile(source, fn, 'exec')
  240.     dis(code)
  241.  
  242. if __name__ == '__main__':
  243.     _test()
  244.  
  245.